package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtAccessoryType;
import com.hilaver.dzmis.codetable.CtApplicationType;
import com.hilaver.dzmis.product.ProductAccessoryHang;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class ProductAccessoryHangServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "productIdentification.id";
	
	public final static String COLUMN_ACCESSORY_OTHERS_ID = "aoId";
	
	public final static String COLUMN_ACCESSORY_OTHERS_REFERENCE = "aoReference";
	
	public final static String COLUMN_ACCESSORY_OTHERS_CT_BRAND = "ctBrand";
	
	public final static String COLUMN_ACCESSORY_OTHERS_CT_CATEGORY = "ctAccessoryType";
	
	public final static String COLUMN_ACCESSORY_OTHERS_CT_MATERIAL = "ctMaterial";
	
	private CtServiceImpl ctService;
	private BiAccessoryOthersServiceImpl biAccessoryOthersServiceImpl;
	
	public ProductAccessoryHangServiceImpl(){
		this.ctService = new CtServiceImpl();
		this.biAccessoryOthersServiceImpl = new BiAccessoryOthersServiceImpl();
	}
	
	public String edit(ProductAccessoryHang hang) throws Exception{
		String operation = null;
		if(hang.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(hang);
		super.editSysMbox(hang, operation);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(ProductAccessoryHang.class.getName(), id);
		return super.delete(ProductAccessoryHang.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductAccessoryHang obj = (ProductAccessoryHang)this.dao.get(ProductAccessoryHang.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(this.getOtherInfoStr(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = COLUMN_PI_ID + "=" + piId;
		List<ProductAccessoryHang> objList = this.dao.getAll(ProductAccessoryHang.class.getName(), COLUMN_ID, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (ProductAccessoryHang obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(this.getOtherInfoStr(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductAccessoryHang.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	private String getOtherInfoStr(ProductAccessoryHang hang) throws Exception{
		StringBuffer sb = new StringBuffer();
		
		sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + "1>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(hang.getBiAccessoryOthers1().getId());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + "1>");
		
		sb.append("<ctAccessoryType1>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), hang.getBiAccessoryOthers1().getCtAccessoryType(), locale));
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctAccessoryType1>");
		
		sb.append("<accessoryReference1>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(hang.getBiAccessoryOthers1().getAccessoryReference()==null?"":hang.getBiAccessoryOthers1().getAccessoryReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</accessoryReference1>");
		
		sb.append("<customerReference1>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(hang.getBiAccessoryOthers1().getCustomerReference()==null?"":hang.getBiAccessoryOthers1().getCustomerReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</customerReference1>");
		
		sb.append("<" + COLUMN_PHOTO_NAME + ">");
		sb.append(Constants.XML_CDATA[0]);
		if (hang.getPhotoName() == null || "".equals(hang.getPhotoName())) {
			sb.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + hang.getPhotoName(), hang.getPhoto());
			sb.append(hang.getPhotoName());
		}
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</" + COLUMN_PHOTO_NAME + ">");
		

		if(hang.getBiAccessoryOthers2() != null){
			sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + "2>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers2().getId());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + "2>");
			
			sb.append("<ctAccessoryType2>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), hang.getBiAccessoryOthers2().getCtAccessoryType(), locale));
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</ctAccessoryType2>");
			
			sb.append("<accessoryReference2>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers2().getAccessoryReference()==null?"":hang.getBiAccessoryOthers2().getAccessoryReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</accessoryReference2>");
			
			sb.append("<customerReference2>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers2().getCustomerReference()==null?"":hang.getBiAccessoryOthers2().getCustomerReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</customerReference2>");	
		}
		
		
		if(hang.getBiAccessoryOthers3() != null){
			sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + "3>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers3().getId());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + "3>");
			
			sb.append("<ctAccessoryType3>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), hang.getBiAccessoryOthers3().getCtAccessoryType(), locale));
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</ctAccessoryType3>");
			
			sb.append("<accessoryReference3>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers3().getAccessoryReference()==null?"":hang.getBiAccessoryOthers3().getAccessoryReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</accessoryReference3>");
			
			sb.append("<customerReference3>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers3().getCustomerReference()==null?"":hang.getBiAccessoryOthers3().getCustomerReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</customerReference3>");	
		}
		
		if(hang.getBiAccessoryOthers4() != null){
			sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + "4>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers4().getId());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + "4>");
			
			sb.append("<ctAccessoryType4>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), hang.getBiAccessoryOthers4().getCtAccessoryType(), locale));
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</ctAccessoryType4>");
			
			sb.append("<accessoryReference4>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers4().getAccessoryReference()==null?"":hang.getBiAccessoryOthers4().getAccessoryReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</accessoryReference4>");
			
			sb.append("<customerReference4>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers4().getCustomerReference()==null?"":hang.getBiAccessoryOthers4().getCustomerReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</customerReference4>");
		}

		if(hang.getBiAccessoryOthers5() != null){
			sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + "5>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers5().getId());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + "5>");
			
			sb.append("<ctAccessoryType5>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), hang.getBiAccessoryOthers5().getCtAccessoryType(), locale));
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</ctAccessoryType5>");
			
			sb.append("<accessoryReference5>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers5().getAccessoryReference()==null?"":hang.getBiAccessoryOthers5().getAccessoryReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</accessoryReference5>");
			
			sb.append("<customerReference5>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers5().getCustomerReference()==null?"":hang.getBiAccessoryOthers5().getCustomerReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</customerReference5>");	
		}
		
		if(hang.getBiAccessoryOthers6() != null){
			sb.append("<" + COLUMN_ACCESSORY_OTHERS_ID + "6>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers6().getId());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</" + COLUMN_ACCESSORY_OTHERS_ID + "6>");
			
			sb.append("<ctAccessoryType6>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(this.ctService.getDescription(CtAccessoryType.class.getName(), hang.getBiAccessoryOthers6().getCtAccessoryType(), locale));
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</ctAccessoryType6>");
			
			sb.append("<accessoryReference6>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers6().getAccessoryReference()==null?"":hang.getBiAccessoryOthers6().getAccessoryReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</accessoryReference6>");
			
			sb.append("<customerReference6>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(hang.getBiAccessoryOthers6().getCustomerReference()==null?"":hang.getBiAccessoryOthers6().getCustomerReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</customerReference6>");	
		}
		return sb.toString();
	}

	public static void main(String[] args) throws Exception{
		ProductAccessoryHangServiceImpl os = new ProductAccessoryHangServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}
}



